package com.example.wo.gdmapapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener {
    MapView mMapView = null;
    private AMap aMap;
    private EditText edtkey;
    private Button btnSearch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.map);
        aMap =mMapView.getMap();
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(new
                CameraPosition(new LatLng(23.18321,113.353111),15,30,0 ));
        aMap.moveCamera(mCameraUpdate);
        aMap.setOnMapLongClickListener(this);
        initSearch();

    }

    private void initSearch() {
        edtkey = findViewById(R.id.edt_key);
        btnSearch = findViewById(R.id.btn_search);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key = edtkey.getText().toString();
                PoiSearch.Query query = new PoiSearch.Query(key,"","020");
                query.setPageSize(10);// 设置每页最多返回多少条poiitem
                query.setPageNum(0);//设置查询页码
                PoiSearch poiSearch = new PoiSearch(MainActivity.this, query);
                poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(23.18321,113.35311),2000 ));
                poiSearch.setOnPoiSearchListener(MainActivity.this);
                poiSearch.searchPOIAsyn();
            }
        });
    }

    public void onMapLongClick(LatLng latLng){
        Log.e("","");
        Marker marker =aMap.addMarker(new MarkerOptions().position(latLng)
                .title("广交院")
                .snippet("欢迎光临"));
    }
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        Log.e("MainActivity","onPoiSearched");
        if(poiResult ==null){
            Log.e("MainActivity","aa"+i);

            /*poiResult.getSearchSuggestionKeywords()+"  "
                    +poiResult.getSearchSuggestionCitys()*/

            return;
        }
        ArrayList<PoiItem> poiItemsList = poiResult.getPois();
        for(PoiItem poiItem:poiItemsList){
            Log.e("MainAcitivty",poiItem.getTitle());
            double latitude = poiItem.getLatLonPoint().getLatitude();
            double longitude =poiItem.getLatLonPoint().getLongitude();
            LatLng latLng = new LatLng(latitude,longitude);
            Log.e("onPoiSearched",""+latLng);
            aMap.addMarker(new MarkerOptions().position(latLng)
                    .title(poiItem.getTitle())
                    .snippet(poiItem.getTel()));
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
